package com.ruoyi.common.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

public class CreateCode {

    public static String getFormatTime(String format) {
        LocalDate currentDate = LocalDate.now();
        int year = currentDate.getYear();
        String month = String.valueOf(currentDate.getMonthValue());
        if (month.length() == 1) {
            month = String.format("%0" + 2 + "d", Integer.parseInt(month));
        }
        String day = String.valueOf(currentDate.getDayOfMonth());
        if (day.length() == 1) {
            day = String.format("%0" + 2 + "d", Integer.parseInt(month));
        }
        String dateTime = year + month + day;
        return format + dateTime;
    }

    public static String authCreateCodeNotNull(String format, String code) {
        String index = code.substring(format.length() + 8, code.length());
        String incry = String.valueOf(Integer.parseInt(index) + 1);
        if (incry.length() == 1) {
            incry = String.format("%0" + 2 + "d", Integer.parseInt(incry));
        }
        String formatCode = code.substring(0, format.length() + 8) + incry;
        return formatCode;
    }


    public static String authCreateCodeIfNull(String formatDate) {
        String formatCode = formatDate + "01";
        return formatCode;
    }

    public static String formatDate(String path) {
        int i = path.indexOf("_") + 1;
        String inputDate = path.substring(i, i + 8);
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd");
        String formattedDate = null;
        try {
            Date date = inputFormat.parse(inputDate);
            formattedDate = outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formattedDate + "/" + path;
    }


}
